﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MMS
{
    public partial class FormConfig : Form
    {
        private BackgroundWorker backgroundWorker = new BackgroundWorker();
        private FormLoading formLoading = new FormLoading();
        public FormConfig()
        {
            InitializeComponent();
            this.Text = Utils.APP_TITLE;
            this.AcceptButton = btnTestConn;
            this.CancelButton = btnExit;
            backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
            backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {
                this.Activate();
                formLoading.Hide();
                Result result = (Result)e.Result;
                if (result.IsSuccess)
                    Utils.ShowNotication(result.Message);
                else
                    Utils.ShowError(result.Message);
            }
            catch (Exception exception)
            {
                Utils.ShowError(exception.Message);
                formLoading.Close();
            }
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Result result = new Result();
            try
            {
                SqlConnection sqlConnection = new SqlConnection(getConnectionString());
                sqlConnection.Open();
                result.IsSuccess = true;
                result.Message = @"Kết nối thành công";
            }
            catch (Exception exception)
            {
                result.IsSuccess = false;
                result.Message = exception.Message;
            }
            finally
            {
                e.Result = result;
            }
        }

        private string getConnectionString()
        {
            return string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}",
                        txtServerName.Text, txtDBName.Text, txtUserName.Text, txtUserPass.Text);
        }

        private void control_Click(object sender, EventArgs e)
        {
            string controlName = ((Control)sender).Name;
            if (controlName == btnTestConn.Name)
            {
                if (formLoading.IsDisposed)
                    formLoading = new FormLoading();
                formLoading.Title = @"Đang kết nối đến máy chủ. Xin vui lòng đợi...";
                backgroundWorker.RunWorkerAsync();
                formLoading.ShowDialog();
            }
            else if (controlName == btnSave.Name)
            {
                Utils.SetConfigParam(Param.ConnectionString, getConnectionString());
            }
            else if (controlName == btnExit.Name)
            {
                this.Close();
            }
        }

        private void FormConfig_Load(object sender, EventArgs e)
        {

        }
    }
}
